package com.webServer.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/*完成解析请求的工作*
    请求行
    消息头
    消息正文/
 */
public class HttpServletRquest {
    private Socket socket;

    //请求行相关信息
    private String method;//请求方式
    private String uri;//抽象路径
    private String protocol;//协议版本
    private Map<String,String> headers= new HashMap<>(); //消息头


    public HttpServletRquest(Socket socket) throws IOException {
        this.socket=socket;

        //1、1解析请求行
        parseRequestLine();

        //1、2解析消息头
        parseHeaders();

        //1.3解析消息正文
        parseContent();
    }

    private  String readLine() throws IOException {
        InputStream in = socket.getInputStream();
        char pre='a',cur='a';//pre上次读取的字符，cur本次读取的字符
        StringBuilder builder = new StringBuilder();
        int d;
        while((d = in.read())!=-1){
            cur = (char)d;//本次读取到的字符
            if(pre==13&&cur==10){//判断是否连续读取到了回车和换行符
                break;
            }
            builder.append(cur);
            pre = cur;//在进行下次读取字符前将本次读取的字符记作上次读取的字符
        }return  builder.toString().trim();
    }

    /*解析请求行*/
    private void parseRequestLine() throws IOException {
        //读取客户端发过来的消息
        String line=readLine();
        System.out.println(line);

        String[] array = line.split("\\s");
        method = array[0];
        uri = array[1];//这里可能会出现数组下标越界异常!原因:浏览器空请求，后期会解决
        protocol = array[2];
        //http://localhost:8088/myweb/index.html
        System.out.println("method:"+method);    //GET
        System.out.println("uri:"+uri);          // /myWeb/index.html
        System.out.println("protocol:"+protocol);//HTTP/1.1

    }

    /*解析消息头*/
    private void parseHeaders() throws IOException {
        //1.2解析消息头

        while(true){
            String line=readLine();
            if(line.isEmpty()){
                break;
            }
            System.out.println("消息头："+line);
            //将消息头按照“冒号空格”拆分为消息头的名字和值并以key，value存入headers
            String[] array =line.split(":\\s");
            array=line.split(":\\s");
            headers.put(array[0],array[1]);
        }
        System.out.println("headers="+headers);
    }

    /*解析消息正文*/
    private void parseContent(){}

    public String getUri() {
        return uri;
    }

    public String getProtocol() {
        return protocol;
    }

    /**/
    public String getHeaders(String name) {
        return headers.get(name);
    }
}
